package com.example.abe.diandi.home.activity;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;
import com.example.abe.diandi.base.BaseActivity;
import com.example.abe.diandi.category.model.CategoryModel;
import com.example.abe.diandi.home.fragment.MainDrawerFragment;
import com.example.abe.diandi.home.view.MainAddNoteView;
import com.example.abe.diandi.home.view.MainSortPopupWindow;
import com.example.abe.diandi.main.AppConfig;
import com.example.abe.diandi.trends.activity.SearchTrendActivity;
import com.example.abe.diandi.trends.activity.TrendsEditActivity;
import com.example.abe.diandi.trends.fragment.TrendsFragment;

public class HomeActivity extends BaseActivity {

    @BindView(R.id.act_main_drawer_start)
    protected FrameLayout mFlStart;

    @BindView(R.id.act_main_fl_content)
    protected FrameLayout mFlContent;

    @BindView(R.id.act_main_drawer_layout)
    protected DrawerLayout mDrawerLayout;

    @BindView(R.id.common_title_bar_ll_title)
    protected LinearLayout mLlTitle;

    @BindView(R.id.act_main_view_main_add_note_view)
    protected MainAddNoteView mAddNoteView;

    @BindView(R.id.common_title_bar_tv_title)
    protected TextView mTvTitle;

    protected MainDrawerFragment mFragmentUserInfo;
    protected TrendsFragment mFragmentTrends;
    protected MainSortPopupWindow mSortPopupWindow;

    @Override
    public int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
    }

    @Override
    public void onResume(){
        super.onResume();
        mAddNoteView.setState(MainAddNoteView.State.CLOSE);
    }

    @Override
    public void initView() {
        mFragmentTrends = TrendsFragment.instance();
        mFragmentTrends.setCategoryModel(AppConfig.getCategoryList().get(0));
        replaceFragment(mFlContent, mFragmentTrends);
        mFragmentUserInfo = MainDrawerFragment.instance();
        replaceFragment(mFlStart, mFragmentUserInfo);
        mSortPopupWindow = new MainSortPopupWindow(this, AppConfig.getCategoryList());
        mSortPopupWindow.setOnItemClickListener(onPopupWindowClickListener);
        mAddNoteView.setOnItemClickListener(onAddNoteClickListener);
    }

    private MainSortPopupWindow.OnItemClickListener onPopupWindowClickListener =  new MainSortPopupWindow.OnItemClickListener() {
        @Override
        public void onItemClick(CategoryModel categoryModel) {
            mTvTitle.setText(categoryModel.getTitle());
            mFragmentTrends.setCategoryModel(categoryModel);
            mFragmentTrends.setStartRefresh();
        }
    };

    private MainAddNoteView.OnItemClickListener onAddNoteClickListener = new MainAddNoteView.OnItemClickListener() {
        @Override
        public void onItemClick(String text) {
            Intent intent = new Intent(HomeActivity.this, TrendsEditActivity.class);
            switch (text){
                case "图片":
                    intent.putExtra(TrendsEditActivity.EDIT_TYPE, TrendsEditActivity.EDIT_IMAGE);
                    break;
                case "文字":
                    intent.putExtra(TrendsEditActivity.EDIT_TYPE, TrendsEditActivity.EDIT_TEXT);
                    break;
            }
            startActivity(intent);
        }
    };

    @OnClick(R.id.common_title_bar_btn_left)
    protected void onDrawerStartClick(){
        mDrawerLayout.openDrawer(Gravity.LEFT);
    }

    @OnClick(R.id.common_title_bar_ll_title)
    protected void onSortClick(){
        mSortPopupWindow.showAsDropDown(mLlTitle);
    }

    @OnClick(R.id.common_title_bar_iv_right)
    protected void onSearchClick(){
        Intent intent = new Intent(this, SearchTrendActivity.class);
        startActivity(intent);
    }
}
